package com.longxi.tianjintianbao.ui.adapter.multitype.completeEnter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.listener.SelectEnterEditTextListener;
import com.longxi.tianjintianbao.model.completeEnter.EnterEditText;

import me.drakeet.multitype.ItemViewBinder;

/**
 * Created by cxw on 2018/2/20.
 */
public class EnterEditTextViewBinder extends ItemViewBinder<EnterEditText, EnterEditTextViewBinder.ViewHolder> {

    SelectEnterEditTextListener listener;
    public EnterEditTextViewBinder(SelectEnterEditTextListener selectEnterEditTextListener) {
        listener = selectEnterEditTextListener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_enter_edittext, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull EnterEditText enterEditText) {
        holder.mEtContent.setHint(enterEditText.getHintValue());

        if(!TextUtils.isEmpty(enterEditText.getValue())){
            holder.mEtContent.setText(enterEditText.getValue());
        }

        holder.mEtContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                enterEditText.setValue(holder.mEtContent.getText().toString());
                listener.onSuccess(enterEditText);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        EditText mEtContent;
        ViewHolder(View itemView) {
            super(itemView);
            mEtContent = itemView.findViewById(R.id.etContent);
        }
    }
}
